2020-2021 Sunseeker Telemetry and Lighting System
usci_b_i2c_ex4_masterRxMultiple.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 #include "driverlib.h"
33 
34 //*****************************************************************************
52 //
53 //*****************************************************************************
54 
55 //*****************************************************************************
56 //
57 //Set the address for slave module. This is a 7-bit address sent in the
58 //following format:
59 //[A6:A5:A4:A3:A2:A1:A0:RS]
60 //
61 //A zero in the "RS" position of the first byte means that the master
62 //transmits (sends) data to the selected slave, and a one in this position
63 //means that the master receives data from the slave.
64 //
65 //*****************************************************************************
66 #define SLAVE_ADDRESS 0x48
67 //*****************************************************************************
68 //
69 //Specify Expected Receive data count.
70 //
71 //*****************************************************************************
72 #define RXCOUNT 0x05
73 
74 unsigned char receiveBuffer[10] = { 0x01, 0x01, 0x01, 0x01, 0x01,
75  0x01, 0x01, 0x01, 0x01, 0x01};
76 unsigned char *receiveBufferPointer;
77 unsigned char receiveCount = 0;
78 
79 void main ()
80 {
81  //Stop WDT
82  WDT_A_hold(WDT_A_BASE);
83 
84  //Assign I2C pins to USCI_B0
85  GPIO_setAsPeripheralModuleFunctionInputPin(
86  GPIO_PORT_P3,
87  GPIO_PIN1 + GPIO_PIN2
88  );
89 
90  //Initialize Master
91  USCI_B_I2C_initMasterParam param = {0};
92  param.selectClockSource = USCI_B_I2C_CLOCKSOURCE_SMCLK;
93  param.i2cClk = UCS_getSMCLK();
94  param.dataRate = USCI_B_I2C_SET_DATA_RATE_100KBPS;
95  USCI_B_I2C_initMaster(USCI_B0_BASE, &param);
96 
97  //Specify slave address
98  USCI_B_I2C_setSlaveAddress(USCI_B0_BASE,
100  );
101 
102  //Set receive mode
103  USCI_B_I2C_setMode(USCI_B0_BASE,
104  USCI_B_I2C_RECEIVE_MODE
105  );
106 
107  //Enable I2C Module to start operations
108  USCI_B_I2C_enable(USCI_B0_BASE);
109 
110  //Enable master Receive interrupt
111  USCI_B_I2C_enableInterrupt(USCI_B0_BASE,
112  USCI_B_I2C_RECEIVE_INTERRUPT
113  );
114 
115  //wait for bus to be free
116  while (USCI_B_I2C_isBusBusy(USCI_B0_BASE )) ;
117 
118  while (1)
119  {
120  receiveBufferPointer = (unsigned char *)receiveBuffer;
122 
123  //Initialize multi reception
124  USCI_B_I2C_masterReceiveMultiByteStart(USCI_B0_BASE);
125 
126  //Enter low power mode 0 with interrupts enabled.
127  __bis_SR_register(LPM0_bits + GIE);
128  __no_operation();
129  }
130 }
131 
132 //******************************************************************************
133 //
134 //This is the USCI_B0 interrupt vector service routine.
135 //
136 //******************************************************************************
137 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
138 #pragma vector=USCI_B0_VECTOR
139 __interrupt
140 #elif defined(__GNUC__)
141 __attribute__((interrupt(USCI_B0_VECTOR)))
142 #endif
143 void USCI_B0_ISR (void)
144 {
145  switch (__even_in_range(UCB0IV,12)){
146  case USCI_I2C_UCRXIFG:
147  {
148  //Decrement RX byte counter
149  receiveCount--;
150 
151  if (receiveCount){
152  if (receiveCount == 1) {
153  //Initiate end of reception -> Receive byte with NAK
155  USCI_B_I2C_masterReceiveMultiByteFinish(
156  USCI_B0_BASE
157  );
158  }
159  else {
160  //Keep receiving one byte at a time
161  *receiveBufferPointer++ = USCI_B_I2C_masterReceiveMultiByteNext(
162  USCI_B0_BASE
163  );
164  }
165  }
166  else {
167  //Receive last byte
168  *receiveBufferPointer = USCI_B_I2C_masterReceiveMultiByteNext(
169  USCI_B0_BASE
170  );
171  __bic_SR_register_on_exit(LPM0_bits);
172  }
173  break;
174  }
175  }
176 }
177 
MPU_initThreeSegmentsParam param
__no_operation()
__bic_SR_register_on_exit(LPM3_bits|GIE)
unsigned char receiveCount
unsigned char * receiveBufferPointer
unsigned char receiveBuffer[10]
void USCI_B0_ISR(void)
#define SLAVE_ADDRESS